//
//  CZOauthViewController.swift
//  chengweibo
//
//  Created by apple on 16/3/5.
//  Copyright © 2016年 Wind. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZOauthViewController: UIViewController {

    override func loadView() {
        //设置webview加载界面
        self.view = webview
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
      

        self.view.backgroundColor = UIColor.whiteColor()
        
        //设置代理
        webview.delegate = self
        
        //创建导航栏的按钮
     setupNavigationBar()
        
        //加载授权界面.
        let request = NSURLRequest(URL:CZNetworkTool.sharedInstance
        .oauthLoginUrl())
        webview.loadRequest(request)
        
        
        
    }
    
    //导航栏按钮的创建方法
    func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFill")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
       
    }
    
    //导航栏按钮的点击事件
    func autoFill () {
        let js = "document.getElementById('userId').value='13450111878';document.getElementById('passwd').value='WCFwuchengfeng6'"
        webview.stringByEvaluatingJavaScriptFromString(js)
    }
    
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //webview的懒加载
    lazy var webview :UIWebView = UIWebView()
}

extension CZOauthViewController : UIWebViewDelegate {
    
    //webview开始加载请求
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.showWithStatus("正在加载登录界面", maskType: SVProgressHUDMaskType.Black)
        
        
    }
    //加载请求完毕时调用
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    //webeviw加载请求失败是调用
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.showWithStatus("登录界面加载失败", maskType: SVProgressHUDMaskType.Black)
    }
    //webview询问我们是否要去加载某个请求的时候就去调用这个方法
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        
        return true
}


}

